<cfcomponent output="false">
	
	<!--- --->
		
	<cffunction name="renderSupportFeedback" access="public" output="true" returntype="string">
		
		<cfset var local = {} />
		
		<cfset arguments.query = $.run.it('discussionService.getActiveDiscussions',{order="discussion.archive asc, message_date.max_date desc"}) />
		<cfset local.new_count = 0 />
		<cfset local.archive_count = 0 />
		<!--- count new messages --->
		<cfloop query="arguments.query">
			<cfset local.new_count += arguments.query.unread />
			<cfif arguments.query.archive eq 1 and arguments.query.unread eq 0>
				<cfset local.archive_count++ />
			</cfif>
		</cfloop>

		<cfsavecontent variable="local.result">
			<cfset local.discussionID = $.request.getValue('discussionID')>
			<cfif local.discussionID neq "">
				<!--- open this discussion, probably just followed it --->
				<script>
					$(document).ready(function(){
						var $discussion = $('.feedback-open-item[discussionid=#local.discussionID#]');
						var $expand = $discussion.find('i.fa-plus-square-o');
						var $respond = $discussion.find('[name=respond-message]');
						Feedback.expandDiscussion($expand.get(0));
						Feedback.showRespond($respond.get(0));
						<!--- check for existing message --->
					});
				</script>
			</cfif>

			<div class="module-left-element first" id="module-left-feedback">
				<div class="module-left-title">
					<i class="fa fa-bug" style="font-size:14px;"></i>Support / Feedback<i style="font-size:14px;vertical-align:top;" class="fa fa-comments-o"></i>
					<cfif local.new_count gt 0>
						<span class="new">(#local.new_count#)</span>
					</cfif>
				</div>

				<cfif structKeyExists(url,"feedback")>
					<i id="feedback-toggle" class="fa fa-toggle-up"></i>
					<div class="feedback-contents" style="">
				<cfelse>
					<i id="feedback-toggle" class="fa fa-toggle-down"></i>
					<div class="feedback-contents closed" style="display:none;">
				</cfif>
				
				<div class="feedback-open">
					#$.run.it('discussionView.renderOpenDiscussions',{query=arguments.query,discussionID=local.discussionID})#
				</div>
				
				<!--- link for archived --->
				<cfif local.archive_count gt 0>
					<div style="background-color:##e2e2e2;padding-left:7px;">
						<a href="javascript:void(0);" onclick="Feedback.showArchived(this);">show #local.archive_count# archived</a>
					</div>
				</cfif>
				
				<div class="feedback-tabs">
					<div id="feedback-tab-ask" discussionCategoryID="E3CDB6ED83AF11E3B691A0481CC65D25" class="feedback-tab selected">
						<i class="fa fa-question-circle"></i><div class="feedback-tab-text">Ask a Question</div>
					</div>
					<div id="feedback-tab-praise" discussionCategoryID="03F6798783B011E3B691A0481CC65D25" class="feedback-tab">
						<i class="fa fa-thumbs-up"></i><div class="feedback-tab-text">High-Five Us</div>
					</div>
					<div id="feedback-tab-idea" discussionCategoryID="E6509F9083AF11E3B691A0481CC65D25" class="feedback-tab">
						<i class="fa fa-lightbulb-o"></i><div class="feedback-tab-text">Suggest an Idea</div>
					</div>
					<div id="feedback-tab-bug" discussionCategoryID="FE64C01B83AF11E3B691A0481CC65D25" class="feedback-tab">
						<i class="fa fa-bug"></i><div class="feedback-tab-text">Report a Bug</div>
					</div>
				</div>
				
				#$.render.form(name="support_feedback_form",method="discussionController.saveDiscussion",onsubmit="return Feedback.trySubmit(this);")#
					<div class="feedback-title ask">
						Got questions about anything? Ask it here.
					</div>
					<div class="feedback-title praise hidden">
						Tell us what we're doing right. It would make our day!
					</div>
					<div class="feedback-title idea hidden">
						Got a suggestion that would make Craftivity better? Let us know.
					</div>
					<div class="feedback-title bug hidden">
						Sorry, is something not working as expected? Tell us and we'll patch it up ASAP.
					</div>			
				
					<input type="hidden" name="discussionCategoryID" value="E3CDB6ED83AF11E3B691A0481CC65D25" />

					<div class="feedback-input">
						<div class="feedback-input-input"><input validate="required" name="name" value="" autocomplete="off" onfocus="Feedback.clearSuggestions(this);" placeholder="Subject" validate="maxLength200" /></div>
					</div>
					
					<div class="feedback-input">
						<div class="feedback-input-input"><textarea validate="required" name="message" placeholder="Message">	</textarea></div>
					</div>
					
					<button class="red small" name="submit-discussion" type="button" onclick="Feedback.trySubmit(this);">submit</button>
					<button class="grey small hidden" name="submit-discussion-wait" type="button" onclick="void(0);">
						<i class="fa fa-spin fa-spinner"></i>wait
					</button>
				</form>
				
				<div class="feedback-suggestions" style="display:none;"></div>
				</div>
				<div class="module-left-element-basebar"></div>
			</div>
		</cfsavecontent>
		
		<cfreturn $.render.clean(local.result) />
	</cffunction>
	
	<!--- --->
	
	<cffunction name="renderOpenDiscussions" access="public" output="true" returntype="string">
		<cfargument name="query" required="false" default='#$.run.it('discussionService.getActiveDiscussions',{order="discussion.archive asc, message_date.max_date desc"})#' />
		<cfargument name="discussionID" required="false" default="" />

		<cfset var local = {} />
		
		<cfsavecontent variable="local.result">
			<cfloop query="arguments.query">
				<div class="feedback-open-item <cfif arguments.query.archive eq 1 and arguments.query.unread eq 0>archived</cfif>" discussionID="#arguments.query.id#">
					<i class="fa fa-plus-square-o" onclick="Feedback.expandDiscussion(this);"></i>
					<i class="fa fa-minus-square-o hidden" onclick="Feedback.collapseDiscussion(this);"></i>
					<div class="feedback-open-item-title">
						<cfif arguments.query.discussion_category eq 'Praise'>
							<i class="fa fa-thumbs-up"></i>
						<cfelseif arguments.query.discussion_category eq 'Suggestions'>
							<i class="fa fa-lightbulb-o"></i>
						<cfelseif arguments.query.discussion_category eq 'Bugs'>
							<i class="fa fa-bug"></i>
						<cfelse>
							<i class="fa fa-question-circle"></i>
						</cfif>
						#arguments.query.name#
						<cfif request.session.user.admin eq 1>
							<div class="discussion-id-number">## #arguments.query.id_number#</div>
						</cfif>
					</div>
					<div class="feedback-open-item-created">
						<cfif arguments.query.unread gt 0>
							<span class="new">(#arguments.query.unread#)</span>
						</cfif>
						<cfif arguments.query.unread_date neq "">
							#$.format.humanizeDate($.date.applyLocalization(arguments.query.unread_date))#
						<cfelse>
							#$.format.humanizeDate($.date.applyLocalization(arguments.query.created))#
						</cfif>
					</div>
					<div class="feedback-open-messages" style="display:none;"></div>
					<div class="feedback-open-messages-controls hidden">
						#$.render.form(name="add_message_form",method="discussionController.saveMessage")#
							<input type="hidden" name="discussionID" value="#arguments.query.id#" />
							<div>
								<textarea class="hidden feedback-message-textarea" validate="required" name="message" value="" placeholder="Your message"></textarea>
							</div>
							<button class="right small red hidden" type="button" name="submit-message" validate="required" onclick="Feedback.saveMessage('#arguments.query.id#');">Submit</button>
							<button class="right small red" type="button" name="respond-message" onclick="Feedback.showRespond(this);">Respond</button>
							<cfif request.session.user.admin eq 1>
								<button class="right small" type="button" name="archive-message" onclick="Main.func('discussionController','archiveDiscussion','&discussionID=#arguments.query.id#','archive this?');" style="right:71px;bottom:6px;">archive</button>
							</cfif>
							<button class="left small grey" type="button" name="unfollow-discussion" onclick="Feedback.showUnfollow(this);">Unfollow</button>
							<button class="left small grey" style="display:none;" type="button" name="unfollow-discussion-real" onclick="Feedback.unfollowDiscussion('#arguments.query.id#');">Remove from my list</button>
							<button class="left small grey hidden" type="button" name="respond-message-cancel" onclick="Feedback.cancelRespond(this);">cancel</button>
						</form>
						<div style="clear:both;"></div>
					</div>
				</div>
			</cfloop>
		</cfsavecontent>
		
		<cfreturn $.render.clean(local.result) />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="renderMessages" access="public" output="true" returntype="string">
		<cfargument name="discussionID" required="true" />
		
		<cfset var local = {} />
		
		<cfset local.messages = $.run.it('discussionService.getMessages',{discussionID=arguments.discussionID}) />
		
		<!--- loop over messages to find the unread ones --->
		<cfset local.discussionMessageIDs = "" />
		<cfloop query="local.messages">
			<cfif local.messages.readID eq "">
				<cfset local.discussionMessageIDs = listAppend(local.discussionMessageIDs,local.messages.id) />
			</cfif>
		</cfloop>
		<!--- mark as read --->
		<cfset $.run.it('discussionService.markAsRead',{discussionMessageIDs=local.discussionMessageIDs}) />
		
		<cfsavecontent variable="local.result">
			<cfloop query="local.messages">
				<div class="feedback-open-message">
					<div class="feedback-open-message-who<cfif local.messages.user_id eq request.session.user.id> you</cfif>">
						<cfif local.messages.user_id eq request.session.user.id>You<cfelse>#local.messages.name#</cfif>
						<cfif local.messages.business_name neq ""> @ <b>#local.messages.business_name#</b></cfif>
					</div>
					<div class="feedback-open-message-timestamp">
						<cfset local.timestamp = $.date.applyLocalization(local.messages.created) />
						<cfif local.messages.readID eq "">
							<span class="new">new!</span>
						</cfif>
						#$.format.humanizeTime(local.timestamp)# - #$.format.humanizeDate(local.timestamp)#
					</div>
					<div class="feedback-open-message-message">#$.format.paragraph(local.messages.message)#</div>
				</div>
			</cfloop>
			<cfif local.messages.recordcount eq 0>
				<div class="feedback-open-message empty">
					No Messages
				</div>
			</cfif>
		</cfsavecontent>
		
		<cfreturn $.render.clean(local.result) />
	</cffunction>
	
	<!--- --->
	
	<cffunction name="renderSuggestions" access="public" output="true" returntype="string">
		<cfargument name="discussionCategoryID" required="true" />
		<cfargument name="subject" required="true" />
		
		<cfset var local = {} />
		
		<cfset local.suggestions = $.run.it('discussionService.getSimilarDiscussions',{discussionCategoryID=arguments.discussionCategoryID,string=arguments.subject}) />

		<cfsavecontent variable="local.result">
			<div class="feedback-title">Does one of these topics match yours ?</div>
			<cfloop query="local.suggestions">
				<div class="feedback-open-item">
					<div class="feedback-suggestion-header">
						#local.suggestions.follower_count#
						<i class="fa fa-user" bubble="#local.suggestions.follower_count# <cfif local.suggestions.follower_count eq 1>person<cfelse>people</cfif> currently following </br>this discussion"></i>
						&nbsp;&nbsp;&nbsp;#local.suggestions.message_count#
						<i class="fa fa-comment-o" bubble="<cfif local.suggestions.message_count gt 0>Click to see #local.suggestions.message_count# messages<cfelse>No Messages</cfif>" onclick="Feedback.toggleSuggestionMessages($(this));" discussionID="#suggestions.id#" userID="#request.session.user.id#"></i>
					</div>
					</br>
					<div class="feedback-open-item-title" style="padding-left:6px;">#local.suggestions.name#</div>
					<div class="suggestion-messages" style="display:none;" discussionID="#local.suggestions.id#">
					</div>
					#$.render.form(name="follow_discussion",method="discussionController.followDiscussion",onsubmit="Feedback.cookieTheMessageForLater();")#
						<input type="hidden" name="discussionID" value="#local.suggestions.id#" />
						<input type="hidden" name="message" value="" />
						<input type="hidden" name="subject" value="" />
						<button class="red right small" style="bottom:0px;" type="button" onclick="Feedback.followSuggestedDiscussion('#local.suggestions.id#');">follow</button>
						<div style="clear:both;"></div>
					</form>
				</div>
			</cfloop>
			<div class="feedback-title">Don't see your topic?</div>
			<button class="blue small" style="position:relative;margin-top:7px;" type="button" onclick="Feedback.noMatch(this);" >New Topic</button>
		</cfsavecontent>
		
		<cfif local.suggestions.recordcount eq 0>
			<cfreturn "" />
		<cfelse>
			<cfreturn $.render.clean(local.result) />
		</cfif>
		
	</cffunction>
	
	<!--- --->

</cfcomponent>